package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.model.CallBack.HuanhuoTrueCallBack;
import com.banzhi.shop.model.IHuanhuoTrueModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by HT on 2017/5/3.
 * 换货完成接口
 */

public class HuanhuoTrueImpl implements IHuanhuoTrueModel {

    @Override
    public void getData(String id, final HuanhuoTrueCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.finishChangegoodsUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("id", id);
        LogUtils.d("HuanhuoTrueImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        callBack.huanSuccess(jsonobject);
                    } else {
                        callBack.huanError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("HuanhuoTrueImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(78)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
